/*
 *  Copyright 2019-2020 Zheng Jie
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package me.zhengjie.controller.PPMoveController;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import me.zhengjie.annotation.Log;
import me.zhengjie.ppService.PPMoveService;
import me.zhengjie.pojo.PPMoveMissionParameter;
import me.zhengjie.pojo.Resp;
import me.zhengjie.pojo.sqlServer.PPMoveMission;
import me.zhengjie.service.PPMoveMissionService;
import me.zhengjie.service.dto.PPMoveMissionDto;
import me.zhengjie.service.dto.PPMoveMissionQueryCriteria;
import me.zhengjie.utils.PageResult;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author xinglin
 * @website https://eladmin.vip
 * @date 2024-08-03
 **/
@RestController
@RequiredArgsConstructor
@Api(tags = "/api/移库任务管理")
@RequestMapping("/api/ppMoveMission")
public class PPMoveMissionController {

    private final PPMoveMissionService ppMoveMissionService;
    private final PPMoveService ppMoveService;

    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('ppMoveMission:list')")
    public void exportPpMoveMission(HttpServletResponse response, PPMoveMissionQueryCriteria criteria) throws IOException {
        ppMoveMissionService.download(ppMoveMissionService.queryAll(criteria), response);
    }

    @GetMapping
    @Log("查询移库任务")
    @ApiOperation("查询移库任务")
    @PreAuthorize("@el.check('ppMoveMission:list')")
    public ResponseEntity<PageResult<PPMoveMissionDto>> queryPpMoveMission(PPMoveMissionQueryCriteria criteria, Pageable pageable) {
        return new ResponseEntity<>(ppMoveMissionService.queryAll(criteria, pageable), HttpStatus.OK);
    }

    @PostMapping
    @Log("新增移库任务")
    @ApiOperation("新增移库任务")
    @PreAuthorize("@el.check('ppMoveMission:add')")
    public ResponseEntity<Object> createPpMoveMission(@Validated @RequestBody PPMoveMission resources) {
        ppMoveMissionService.create(resources);
        return new ResponseEntity<>(HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改移库任务")
    @ApiOperation("修改移库任务")
    @PreAuthorize("@el.check('ppMoveMission:edit')")
    public ResponseEntity<Object> updatePpMoveMission(@Validated @RequestBody PPMoveMission resources) {
        ppMoveMissionService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @DeleteMapping
    @Log("删除移库任务")
    @ApiOperation("删除移库任务")
    @PreAuthorize("@el.check('ppMoveMission:del')")
    public ResponseEntity<Object> deletePpMoveMission(@RequestBody Integer[] ids) {
        ppMoveMissionService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }


    /**
     * @param ppMoveMissionParameter
     * @return
     */
    @PostMapping("/createMoveMission")
    public Resp createMoveMission(@RequestBody PPMoveMissionParameter ppMoveMissionParameter) {


        try {

            ppMoveService.createPPMoveMission(ppMoveMissionParameter);
            return Resp.builder().msg("移库成功").status(200).build();
        } catch (Exception e) {
            return Resp.builder().msg("移库失败").status(500).build();
        }
    }

}